<?php

/*
Process Add Item (admin_add_item.pro.php)
*/

$rank_check = "7";
include "../global.inc.php";

$game=$_GET['game'];

$item_name = $_POST['item_name'];
$item_desc = $_POST['item_desc'];
$rarity = $_POST['rarity'];
$magic_number = $_POST['magic_number'];
$value = $_POST['value'];
$item_type = $_POST['item_type'];
$item_parts = $_POST['item_parts'];
$sell_in_shops = $_POST['sell_in_shops'];
$sell_in = $_POST['sell_in'];
$item_cost = $_POST['item_cost'];
$max_stock = $_POST['max_stock'];

$battle_item = $_POST['battle_item'];
$battle_type = $_POST['battle_type'];
$battle_uses = $_POST['battle_uses'];
$retired = $_POST['retired'];



$directoryName = "../images/user_images/opg_$game";
if (!file_exists($directoryName)) { mkdir($directoryName, 0777); }

$directoryName2 = "../images/user_images/opg_$game/items";
if (!file_exists($directoryName2)) { mkdir($directoryName2, 0777); }

if ($HTTP_POST_FILES['itemImage']['size'] >= 32000)
{
	die(header(error("admin_add_item.php?game=$game", "Your image must be under 32,000 bytes (or 32kb)")));
}

if (!eregi("image/", $HTTP_POST_FILES['itemImage']['type']))
{
	die(header(error("admin_add_item.php?game=$game", "You should only use .gif, .jpg, and .jpeg files for images. Please convert the image to one of these file types.")));
}

$check = fetch("SELECT id FROM items2 WHERE item_name = '$item_name' AND game = '$game'");

if ((!$item_name) OR (!$item_desc) OR (!$rarity) OR ($rarity <= 0) OR ($rarity > "7") OR (!$magic_number) OR (!$value) OR (!$item_type) OR (!$item_parts))
{
	die(header(error("admin_add_item.php?game=$game", "Please do not leave any required information blank!")));
}

if (($item_type != "food") AND 
($item_type != "health_potion") AND 
($item_type != "battle_item") AND 
($item_type != "level_inc") AND 
($item_type != "strength_inc") AND 
($item_type != "speed_inc") AND 
($item_type != "intel_inc") AND 
($item_type != "toy") AND 
($item_type != "pet_look") AND 
($item_type != "defence_inc"))
{
	die(header(error("admin_add_item.php?game=$game", "Please only choose an item type from the menu!")));
} 

if ($sell_in_shops == 1)
{
	$findShop = fetch("SELECT id FROM official_shops2 WHERE id = '$sell_in' AND game = '$game'");
	if (!$findShop[id])
	{
		die(header(error("admin_add_item.php?game=$game", "Please choose a shop that is in your game!")));
	}
}

if ($item_cost < 0) { $item_cost = 0; }
if ($max_stock < 0) { $max_stock = 0; }

if (($battle_item == 1) AND (!$battle_type) OR (!$magic_number) OR (!$battle_uses))
{
	if (($battle_type != "attack") AND 
	($battle_type != "defence") AND 
	($battle_type != "lower_acc") AND 
	($battle_type != "raise_acc") AND 
	($battle_type != "double_damage"))
	{
		die(header(error("admin_add_item.php?game=$game", "Please only choose a battle type from the menu!")));
	} 
	
	die(header(error("admin_add_item.php?game=$game", "Please do not leave any required information blank!")));
}

if ($magic_number < 0) { $magic_number = 0; }

if ($check[id])
{
	header("Location: admin_add_item.php?game=$game&error=There+is+already+an+item+with+the+name+$item_name.");
}
else
{
	mysql_query("INSERT INTO items2 (item_name,item_type,item_value,item_rarity,item_desc,retired,magic_num,parts,game) VALUES ('$item_name','$item_type','$value','$rarity','$item_desc','$retired','$magic_number','$item_parts','$game')");
	
	$insert_id = mysql_insert_id();
	
	$item_image = "item_" . $insert_id . ".gif";
	
	mysql_query("UPDATE items2 SET item_image = '$item_image' WHERE id = '$insert_id' AND game = '$game'");
	
	if ($sell_in_shops == 1)
	{
		mysql_query("INSERT INTO shop_items2 (item_name,sell_for,cur_stock,max_stock,sell_in,game) VALUES ('$item_name','$item_cost','$max_stock','$max_stock','$sell_in','$game')");
	}
	
	if ($battle_item == 1)
	{
		mysql_query("INSERT INTO battle_items2 (item_id,battle_type,magic_num,uses,game) VALUES ('$insert_id','$battle_type','$magic_number','$battle_uses','$game')");
	}
	
	$file = $HTTP_POST_FILES['itemImage']['tmp_name'];
	$dest = "../images/user_images/opg_$game/items/item_$insert_id.gif";
	
	copy($file, $dest);
	
	header("Location: admin_add_item.php?game=$game&error=The+item+$item_name+has+been+added!");
}

?>
